package com.zhaojonhy.zhaopangwb.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.sina.weibo.sdk.auth.Oauth2AccessToken;
import com.zhaojonhy.zhaopangwb.R;
import com.zhaojonhy.zhaopangwb.utils.AccessTokenKeeper;


/**
 * Created by Administrator on 2016/9/6.
 */
public class SplashActivity extends Activity {

    private static final int WHAT_INTENT2AUTH = 1 ;
    private static final int WHAT_INTENT2MAIN= 2 ;
    private Oauth2AccessToken accessToken ;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what){
                case WHAT_INTENT2AUTH :
                    Intent intent1 = new Intent(SplashActivity.this,OauthActivity.class) ;
                    startActivity(intent1);
                    finish();
                    break;
                case WHAT_INTENT2MAIN :
                    Intent intent2 = new Intent(SplashActivity.this,MainActivity.class) ;
                    startActivity(intent2);
                    finish();
                    break;
                default:
                    break;
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);

        accessToken = AccessTokenKeeper.readAccessToken(this) ;
        if (accessToken.isSessionValid()){
            if (System.currentTimeMillis() > accessToken.getExpiresTime()){
                handler.sendEmptyMessageDelayed(WHAT_INTENT2AUTH,1000) ;
            }else{
                handler.sendEmptyMessageDelayed(WHAT_INTENT2MAIN,1000) ;
            }

        }else {
            handler.sendEmptyMessageDelayed(WHAT_INTENT2AUTH,1000) ;
        }
    }
}
